package com.nannybear.Model;

import android.app.Activity;
import android.graphics.Color;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.nannybear.R;

import java.util.ArrayList;

/**
 * @brief Show the graphic with new values
 */
public class Graphic {
    /********** DECLARATIONS **********/
	GraphViewSeries exampleSeries;
	GraphView graphView;
	ArrayList<GraphViewData> arrayListData;

    /************ METHODS ************/
    /**
     * @brief constructor
     * @param myActivity : takes the activity in parameter
     */
	public Graphic(Activity myActivity){
		initDatas();
		customizeGraphView(myActivity);	
		graphView.addSeries(exampleSeries); // data
	}

    /**
     * @brief Draw the graphic with the value of the arrayList
     */
	private void initDatas(){
        //TODO : delete that when we have the true values
	    arrayListData = new ArrayList<GraphViewData>();
		arrayListData.add(new GraphViewData(120, 20));
		arrayListData.add(new GraphViewData(100, 30));
		arrayListData.add(new GraphViewData(90, 42));

        //Create a tab with all the value
		GraphViewData[] graphViewData = arrayListData.toArray(new GraphViewData[arrayListData.size()]);
		this.exampleSeries = new GraphViewSeries(graphViewData);
		;
	}

    /**
     * @brief Customize the graphView
     */
	private void customizeGraphView(Activity myActivity) {
		graphView = new LineGraphView(
				myActivity // context
				, " " // heading
		);
		((LineGraphView) graphView).setDrawBackground(true);
		((LineGraphView) graphView).setBackgroundColor(myActivity.getResources().getColor(R.color.graph_line));

		graphView.getGraphViewStyle().setGridColor(Color.WHITE);
		graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.WHITE);
		graphView.getGraphViewStyle().setVerticalLabelsColor(Color.WHITE);
		graphView.getGraphViewStyle().setNumHorizontalLabels(5);
		graphView.getGraphViewStyle().setNumVerticalLabels(4);
	}

    /**
     * @brief Add data to the view
     * @param x : x coordinate
     * @param y : y coordinate
     */
	public void refreshWithNewData(double x, double y){
	
		arrayListData.add(new GraphViewData(x, y));
		GraphViewData[] graphViewData = arrayListData.toArray(new GraphViewData[arrayListData.size()]);
		this.getExampleSeries().resetData(graphViewData);
	}

    /*********** ACCESSORS ***********/
	public GraphViewSeries getExampleSeries() {
		return exampleSeries;
	}

	public void setExampleSeries(GraphViewSeries exampleSeries) {
		this.exampleSeries = exampleSeries;
	}

	public GraphView getGraphView() {
		return graphView;
	}

	public void setGraphView(GraphView graphView) {
		this.graphView = graphView;
	}
	
}
